using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Configuration;
using System.Collections.Specialized;
using AlgorithmsVisualized;
using System.Drawing.Drawing2D;

namespace AlgorithmsVisualized.Controls
{
    [Serializable]
    public class Node : ShapeBase, IComparable<Node>
    {
        private static int _ID;

        private double _value;
        private List<Edge> _adj = new List<Edge>();
        private Node _parent;

        public double Value
        {
            get { return _value; }
            set { _value = value; }
        }
        public Node Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }  
        public List<Edge> Adj
        {
            get { return _adj; }
            set { _adj = value; }
        }
        
        public Node()
        {
        }
        public Node(string ID, double Value)
        {
            _id = ID;
            _value = Value;
            OnRedrawRequest(new EventArgs());
        }
        protected override void RefreshBitmap(Graphics g)
        {
            Rectangle rec = new Rectangle(Location, new Size(Width, Height));

            if (!IsSelected)
            {
                using (Brush b = new SolidBrush(BackColor))
                    g.FillEllipse(b, rec);
                if (IsBold)
                    using (Pen p = new Pen(DrawingPen.Color, 5))
                        g.DrawEllipse(p, rec);
                else
                    g.DrawEllipse(DrawingPen, rec);
            }
            else
            {
                using (Brush b = new SolidBrush(SelectedColor))
                    g.FillEllipse(b, rec);
                if (IsBold)
                    using (Pen p = new Pen(SelectedDrawingPen.Color, 5))
                        g.DrawEllipse(p, rec);
                else
                    g.DrawEllipse(SelectedDrawingPen, rec);
            }
            using (Font f = new Font("Tahoma", 10))
            {
                g.DrawString(ID.ToString(), f, Brushes.Black, new PointF(Location.X + Width / 2 - ID.ToString().Length * 8 / 2, Location.Y + 5));
            }
        }
        public override bool IsInside(Point point)
        {
            return Util.VectorLength(Util.Subtract(Center, point)) <= Width / 2;
        }
        internal static Node CreateDefault(Point pos)
        {
            Node n = new Node("Node " + (_ID++).ToString(), 0.0);
            n.BackColor = Color.White;
            n.Width = 60;
            n.Height = 60;
            Point p = Util.TransformToCenter(pos, n.ScaledWidth, n.ScaledHeight - 13);
            n.Left = p.X;
            n.Top = p.Y;
            return n;
        }

        #region IComparable<Node> Members

        public int CompareTo(Node other)
        {
            if (Value > other.Value)
                return 1;
            else if (Value < other.Value)
                return -1;
            else
                return ID.CompareTo(other.ID);
        }

        #endregion
    }
}
